package config

import (
	"github.com/sirupsen/logrus"
	"os"
	"strings"
)

type Logger struct {
	Type      string
	Path      string
	Level     string
	Stdout    string
	EnabledDB bool
	Cap       uint
}

func (e Logger) Setup() (*logrus.Logger, error) {
	var log = logrus.New()
	log.Out = os.Stdout
	switch strings.ToLower(e.Level) {
	case "trace":
		log.SetLevel(logrus.TraceLevel)
	case "debug":
		log.SetLevel(logrus.DebugLevel)
	case "info":
		log.SetLevel(logrus.InfoLevel)
	case "warning":
		log.SetLevel(logrus.WarnLevel)
	case "error":
		log.SetLevel(logrus.ErrorLevel)
	case "fatal":
		log.SetLevel(logrus.FatalLevel)
	case "panic":
		log.SetLevel(logrus.PanicLevel)
	}
	if e.Stdout == "file" {
		file, err := os.OpenFile(e.Path, os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
		if err == nil {
			log.Out = file
		}
	}
	return log, nil
}

var LoggerConfig = new(Logger)
